package com.learndemo.mvcpvmdemo.normal;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.learndemo.mvcpvmdemo.R;
import com.learndemo.mvcpvmdemo.bean.Account;
import com.learndemo.mvcpvmdemo.callback.MCallback;
import com.learndemo.mvcpvmdemo.mvc.MVCModel;

import java.util.Random;

public class NormalActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView mTvResult;
    private EditText mEtAccount;
    private MVCModel mMvcModel; //持有MVCModel引用
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_normal);
    setTitle("NormalActivity");
    initView();
    mMvcModel = new MVCModel();
}

    private void initView() {
    mTvResult = findViewById(R.id.tv_result);
    mEtAccount = findViewById(R.id.et_account);
    findViewById(R.id.btn_getAccount).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        getAccountData(getUserInput(), new MCallback() {
            @Override
            public void onSuccess(Account account) {
                showSuccessPage(account);
            }

            @Override
            public void onFailed() {
              showFailedPage();
            }
        });
    }

    private String getUserInput(){
    return mEtAccount.getText().toString();
    }
    private void showSuccessPage(Account account){
    mTvResult.setText("用户账号：" + account.getName() + " 用户等级：" + account.getLevel());
    }

    private void showFailedPage(){
    mTvResult.setText("获取用户信息失败");
    }

    private void getAccountData(String accountName , MCallback callback){
        Random random = new Random();
        boolean isSuccess = random.nextBoolean();
        if(isSuccess){
            Account account = new Account();
            account.setName(accountName);
            account.setLevel(100);
            callback.onSuccess(account);
        }else{
            callback.onFailed();
        }

    }
}
